package juc.c_020;/*
   Talk is cheap. Show me the code.
 */

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description
 * @Date 2021/6/8
 * @Author liudl
 */
public class T03_ReentrancLock {
    ReentrantLock lock = new ReentrantLock();
    private void m1(){
        lock.lock();
        try {
            for (int i = 0; i < 5; i++) {
                System.out.println(i+1);
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    private void m2(){
        boolean b = lock.tryLock();
        try {
            System.out.println("m2........." +b);
        } finally {
            if (b)lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T03_ReentrancLock t = new T03_ReentrancLock();
        new Thread(t::m1).start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(t::m2).start();
    }
}
